package com.example.sunwendiapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBhelper extends SQLiteOpenHelper {
    private static final String DB_name="Test.db";
    private static final String TAB_Score="Stuscore_tab";
    private static final String TAB_Question="Question_tab";
    private SQLiteDatabase Test_db;
    public DBhelper(Context context, String name, CursorFactory factory, int version) {
        super(context,DB_name, factory, version);
    }


    public void insert_question(ContentValues value){
        SQLiteDatabase db= getWritableDatabase();
        db.insert(TAB_Question, null, value);
        db.close();
    }
    public void insert_score(ContentValues value){
        SQLiteDatabase db = getWritableDatabase();
        long a = db.insert(TAB_Score, null, value);
    }
    @Override
    public void onCreate(SQLiteDatabase database) {
        this.Test_db = database;
        String CREATE_TAB2 = "create table Stuscore_tab(_id integer primary key autoincrement,score integer,time text)";
        String CREATE_TAB1 = "create table Question_tab(_id integer primary key autoincrement,question text)";
        Test_db.execSQL(CREATE_TAB1);
        Test_db.execSQL(CREATE_TAB2);
    }
    public Cursor query_ques(){
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query(TAB_Question, null, null, null, null, null, null);
        return cursor;
    }
    public Cursor query_score(){
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query(TAB_Score, null, null, null, null, null, null);
        return cursor;
    }
    public void delete_question(long id){
        if(Test_db==null)
            Test_db = getWritableDatabase();
        Test_db.delete(TAB_Question, "_id=?", new String[]{String.valueOf(id)});
    }
    public void close(){
        if(Test_db!=null){
            Test_db.close();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    }

